home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
batchut
/
bathints.zip
/
BH_12.DOC
< prev
next >
Wrap
Text File
|
1989-01-05
|
26KB
|
617 lines
BAT-HINT # 12
**************************************************************************
from the BATHINTS library... part of the BATPOWER CONFERENCE from:
THE PAINFRAME OPUS/FIDO 261/1004
Baltimore, Maryland 1-301-488-7461
**************************************************************************
Piping and Redirection
After a standard start of your computer, your operating system is set such
that the "standard" input device is the keyboard (the "console") and the
standard output device is the monitor (the screen, your video, looks like a
T.V. but there's only one channel). The device name that DOS adopts to
define your keyboard is "con", the console. Your video screen has no
device name in DOS. Normally, all input and output is channeled through
these two devices. But you can redirect both your normal input and your
normal output to other devices. Indeed, most of us do it each day when we
redirect the standard output to the printer, rather than the monitor, when
printing document files.
PC-DOS provides a variety of alternatives to these two devices. You can
redirect the standard input device to a file of information or to an
auxillary computer terminal, and you can redirect the standard output to a
file, a printer, a modem or just about anything that is connected to a port
on your computer system.
Though the CTTY internal comand of command.com is not really a topic for
discussion in this conference, users should be aware that this command can
change both your standard input and output devices to a terminal connected
to the computer. The syntax for the CTTY command is:
CTTY device
where "device" is any standard DOS device name (i.e. aux, com1, com2, com3,
con, nul, etc.). The named device must be able to both input and output
all operations. Specifying:
CTTY con
returns the standard input and output devices to your normal keyboard and
screen. Specifying AUX passes standard input and output to peripherals
attached to the first serial/parallel port, COM2 to the second
serial/parallel port and so on. NUL is a dummy device that can be used to
pass information into the "bit-bucket"... the info goes nowhere. Though
the NUL device is most widely used in the inhibition of information
normally passed to your standard output device (your screen) during batch
file operation, it also finds wide use in modem operations and is used less
discriminately in certain security-type operations. Specifying:
CTTY nul
will effectively negate the normal keyboard and screen as standard I/O
devices, disabling the user from entering commands at the keyboard.
The piping and redirection operators used by DOS are:
< > |
None of the symbols shown above can be used in the naming of files or their
extensions. Generally, the < and > symbols are referred to as redirection
symbols and the | symbol is referred to as a piping symbol. Some
authorities refer to all of the symbols as piping symbols. In this
discussion, the former terminology will be used.
Before considering any examples of how redirection may be employed, note
the following device names, which are reserved by DOS and should not be
used otherwise:
AUX the first serial or parallel port
COM1 the first serial port
COM2 the second serial port
COMx a serial port where "x" = 1, 2, 3, 4 etc.
CON the console/keyboard/monitor; can be used as input or output
device(s)
LPT1 the first parallel port, a printer; equivalent to PRN
LPTx a parallel port, printer, where "x" = 1 or 2
PRN the first parallel port (printer)
NUL a dummy device
Although the device names shown above cannot be used in the creation of
files, you can substitute these device names in place of filenames in
certain circumstances.
Redirecting Standard Output
---------------------------
The three most common uses of redirecting standard output are:
1. printing ASCII files
2. creating ASCII files
3. inhibiting output to the standard device (the screen)
The differences between these three operations lie only the device name (or
filename) employed on the right side of the > symbol and the command which
precedes it. For example, to print a directory listing of the current
subdirectory, to a printer attached to parallel port 1, you could issue the
command:
dir > lpt1 or
dir > prn
Both commands are essentially equivalent, though you will find that if you
use the later command for the first time during a DOS session, the system
will prompt you for the name of the device (lpt1, lpt2, etc.). In general,
the output of the command on the left side of the > symbol will be
redirected to the device (or file) specified on the right side of the >
symbol. To redirect the output of the dir command to a printer attached to
serial port 2, you would only need to change the name of the device to
which standard output is to be redirected, thus:
dir > com2
Virtually any executable command may have its output redirected... though
of course there are cases where you would not want to do that... such as
redirecting the output of a program like Word Perfect to a file or
device... that would simply be nonsense.
To create an ASCII file that contains the output of the dir command, you
need only specify the name of the file. For example, to redirect the
output of the dir command to a file called dir.doc, you could issue the
command:
dir > dir.doc
You should exercise caution when redirecting standard output to a file with
the > symbol, since although the file will be created if it does not
already exist, the file will be overwritten if it does exist. Thus when
using redirection of standard output to a file it is best to test whether
that file already exists... since you may not want its contents
overwritten. Consider the following batch file which tests for the
existance of a file specified as the %1 parameter when the batch file is
called, and bypasses the command to redirect the standard output to that
file if that file exists:
DIRDOC.BAT
----------
echo off
if exist %1 goto oops
dir > %1
goto end
:oops
echo %1 already exists!
:end (include a carriage return at the end of this line)
The proper syntax for this batch file is:
dirdoc filename.ext
where filename.ext is any valid filename with or without an extension
(alphanumeric constructs following the name of a batch file on the command
line are "replaceable parameters" and each is separated from the next by a
space; in the example shown above, only one parameter was given,
"filename.ext", and it is thus the %1 replaceable parameter). Thus, to
redirect to a file called list.txt you would enter the command:
dirdoc list.txt
Notice that the batch file first tests for the existance of the file
specified on the command line (the %1 variable), and if it does exist, the
batch file jumps to the label "oops" and informs the user that the file
already exists. With that information, the user could then run the batch
file again, but specifying a different filename for the standard output.
You should be aware that if no %1 variable was specified when the batch
file was called (i.e. only the name of the batch file was given on the
command line), the result would be a "file creation error" since the
statement dir > %1 is attempting to redirect standard output to a device or
file that was not specified. This problem could be avoided by including
another test before the test for existance... just test whether the %1
variable was specified at the command line, as shown in the example below
(you may want to refer to BatHint # 10 to refresh your memory about the IF
subcommand):
DIRDOC2.BAT
-----------
echo off
if "%1"=="" goto novar
if exist %1 goto oops
dir > %1
goto end
:novar
echo You did not specify a filename...
echo The correct syntax is:
echo dirdoc filename.ext
echo where filename.ext is an